import { Component, Injector } from "@angular/core";
import { IonicPage } from "ionic-angular";
import { BasePage } from "../basePage";
import { UserService } from "../../providers/UserService";
import { StorageService } from "./../../providers/StorageService";
import { PIC_URL } from "./../../providers/Config";

declare var $: any;

/**
 * Generated class for the ShopSearchPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage({ name: "shopSearch" })
@Component({
  selector: "page-shop-search",
  templateUrl: "shop-search.html"
})
export class ShopSearchPage extends BasePage {
  userType: any;
  isToShop: boolean;
  pictureurl = ""; //图片服务器的地址
  search: any;
  service_id: any;
  showAttentio: boolean;
  showAttention: any;
  ShopArr = [];
  nailStyleArr = [];
  modalCtrl: any;
  userinfo: any;
  /**关注 */
  attentionNum: boolean;
  attentionVo = {
    focused_id: null,
    user_id: null
  };
btm:boolean=false
  red: number;
  /**店铺列表参数 */
  GetTabBarShopsAPIVo = {
    local_city: 420100,
    user_id: null,
    search_type: 0,
    page_size: 3
  };

  /**搜索传参数绑定 */
  toppings: any;
  gaming: any;
  serveStyle: any;
  searchGetTheFocus: Number = 0;
  shopleng;
  /*保存用户输入的内容*/
  myInput: string;

  //是否已登录
  isUp: boolean = false;

  haveId: boolean; //无身份隐藏悬浮按钮
  constructor(
    injector: Injector,
    public userService: UserService,
    public storage: StorageService
  ) {
    super(injector);
    this.showAttentio = true;
    this.attentionNum = true;
    // this.isToShop = true;
    this.GetTabBarShopsAPI(0);
    this.pictureurl = PIC_URL;
    this.isToShop = true;
    this.userinfo = this.storage.read("userinfo");
    console.log(this.userinfo);
    //this.attentionVo.user_id = this.GetTabBarShopsAPIVo.user_id = this.userinfo.user_id;
    if (this.userinfo == null) {
      this.isToShop = true;
    } else {
      this.userType = this.userinfo.user_type.split(",");
      if (this.userType.indexOf("3") == -1) {
        console.log("我是店主");
        this.isToShop = true;
      } else {
        this.isToShop = false;
        this.isUp = true;
      }

      this.attentionVo.user_id = this.GetTabBarShopsAPIVo.user_id = this.userinfo.user_id;
      for (let key in this.userinfo) {
        if (key == "user_id" && this.GetTabBarShopsAPIVo.user_id != null) {
          this.GetTabBarShopsAPI(0);
          return;
        } else {
          this.showAttentio = false;
          return;
        }
      }
    }
    console.log("yemian jinru");
  }
  ionViewDidLoad() {
    console.log("页面加载的时候");
    this.searchGetTheFocus = 0;
    if (this.userinfo == null) {
      this.haveId = false;
    } else {
      this.haveId = true;
    }
  }
  ionViewWillEnter() {
    console.log("页面将要进入的时候");
    this.searchGetTheFocus = 0;
    if (this.userinfo == null) {
      this.haveId = false;
    } else {
      this.haveId = true;
    }
  }
  ionViewDidEnter() {
    console.log("页面进入的时候");
  }

  ionViewDidLeave() {
    console.log("离开页面的时候");
  }
  ionViewWillUnload() {
    console.log("页面销毁的时候");
  }

  /*搜索部分的函数 当点击搜索框的时候 */
  getFocus($event) {
    /*使输入框不能得到焦点*/
    $event.target.blur();
    /*跳转到搜索页面*/
    // this.navCtrl.push("SearchPage");
    this.modalCtrl.create("SearchPage").present();
  }

  /**跳转 到美甲店铺 */
  toShopDetail(shop_id) {
    let modal = this.modalCtrl.create("NaiShopDetailPage", {
      user_id: this.attentionVo.user_id,
      shop_id: shop_id
    });
    modal.present();
  }
  //跳转到发布
  add1() {
    let modal = this.modalCtrl.create("AddPage");
    modal.present();
    // this.navCtrl.push("AddPage");
  }
  /**跳转 到服务详情页面 */
  toShopInfo(id, shop_id) {
    this.service_id = id;
    console.log("获取店铺id" + shop_id);
    // this.navCtrl.push("ShopInfoPage", { "id": this.service_id });//不要删了，
    let model = this.modalCtrl.create("ShopInfoPage", {
      id: this.service_id,
      shop_id: shop_id
    });
    model.present();
  }

  /**关注 */
  async unsetAttention(event, is_focus, shop_id) {
    console.log(event);
    this.attentionVo.focused_id = shop_id;
    let res = await this.userService.dofocusAPI(this.attentionVo);
    console.log(res);
    console.log(this.GetTabBarShopsAPIVo.search_type);
    this.GetTabBarShopsAPI(this.GetTabBarShopsAPIVo.search_type);
    if (res.code == 1) {
      let toast = this.toastCtrl.create({
        message: res.msg,
        duration: 3000,
        position: "top"
      });
      toast.present();
    }
  }
  /**下拉刷新 */
  doRefresh(refresher) {
    console.log(refresher);
    console.log("下拉刷新");

    this.GetTabBarShopsAPI(this.search);
    setTimeout(() => {
      console.log("加载完成后，关闭刷新");
      refresher.complete();
      let toast = this.toastCtrl.create({
        message: "刷新完成",
        duration: 1500
      });
      toast.present();
    }, 2000);
  }

  /**上拉加载 */
  async doInfinite(infiniteScroll) {
    this.GetTabBarShopsAPIVo.page_size += 3;
    this.GetTabBarShopsAPI(this.search);
    setTimeout(() => {
      console.log("加载完成后，关闭刷新");
      console.log(this.GetTabBarShopsAPIVo.page_size);
      console.log(this.shopleng);
      infiniteScroll.complete();
      if (this.shopleng < this.GetTabBarShopsAPIVo.page_size) {
        infiniteScroll.enable(false);
        this.btm=true
        // let toast = this.toastCtrl.create({
        //   message: "没有更多数据了",
        //   duration: 1500
        // });

        // toast.present();
      }
      if (this.shopleng == undefined) {
        infiniteScroll.enable(false);
        // let toast = this.toastCtrl.create({
        //   message: "没有更多数据了",
        //   duration: 1500
        // });
        this.btm=true
        // toast.present();
      }
    }, 2000);
  }
  /**获取美甲店铺列表 */
  async GetTabBarShopsAPI(search) {
    this.search = search;
    console.log(this.toppings);
    if (this.toppings) {
      this.GetTabBarShopsAPIVo.search_type = this.search;
    } else if (this.gaming) {
      this.GetTabBarShopsAPIVo.search_type = this.search;
    } else if (this.serveStyle) {
      this.GetTabBarShopsAPIVo.search_type = this.serveStyle;
    } else {
      this.GetTabBarShopsAPIVo.search_type = 0;
    }
    this.red = this.GetTabBarShopsAPIVo.search_type;
    console.log(this.red);

    console.log(this.GetTabBarShopsAPIVo.search_type);
    console.log(this.GetTabBarShopsAPIVo);
    console.log(this.GetTabBarShopsAPIVo);
    /**无搜索条件提示 */
    let result = await this.userService.doGetTabBarShopsAPI(
      this.GetTabBarShopsAPIVo
    );
    // console.log(result.data.length);
    if (this.shopleng != undefined) {
      this.shopleng = result.data.length;
    }
    // this.shopleng=result.data.length
    console.log(result);
    if (result != undefined) {
      if (result.error_code == 0) {
        this.ShopArr = result.data;
        return;
      } else if (result.error_code == 1001) {
        let toast = this.toastCtrl.create({
          message: result.reason,
          duration: 3000,
          position: "top"
        });
        toast.present();
        return;
      }
      console.log(this.nailStyleArr);
    }
  }

  shop() {
    // let modal = this.modalCtrl.create("ShopApprovePage");
    // modal.onDidDismiss(data => {

    // });
    // modal.present();
    // if(this.userinfo.user_id!=null){
    //   this.navCtrl.push("ShopApprovePage");
    // }
    if (this.userinfo == null) {
      let alert = this.alertCtrl.create({
        title: "登录提示",
        subTitle: "未登录，请先登陆",
        buttons: [
          {
            text: "取消",
            handler: data => {}
          },
          {
            text: "确认登录",
            handler: data => {
              let modal = this.modalCtrl.create("LoginPage", { reload: true });
              modal.onDidDismiss(data => {
                // window.reload();
                $(".naniler").remove();
                this.isToShop = false;
              });
              modal.present();
            }
          }
        ]
      });
      alert.present();
    } else if (this.userType.indexOf("3") == -1) {
      this.isToShop = true;
      let modal = this.modalCtrl.create("ShopApprovePage");
      modal.onDidDismiss(data => {});
      modal.present();
    } else {
      this.isToShop = false;
    }
  }
}
